package com.rerun.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rerun.entity.ChatJob;
import com.rerun.entity.ChatRecords;
import com.rerun.enums.ChatRecordsStatus;
import com.rerun.mapper.ChatJobMapper;
import com.rerun.mapper.ChatRecordsMapper;
import com.rerun.service.IChatRecordsService;
import com.rerun.vo.memberUser.ChatRecordsSearchVo;
import com.rerun.vo.memberUser.ChatRecordsVo;
import com.rerun.vo.park.ParkResumeViewVo;
@Service
public class ChatRecordsServiceImpl extends ServiceImpl<ChatRecordsMapper,ChatRecords> implements IChatRecordsService {
	@Autowired
	ChatJobMapper chatJobMapper;
	@Override
	public void addChatRecords(ChatRecords records, boolean isUser) {
		EntityWrapper<ChatRecords> wrapper = new EntityWrapper<>();
		wrapper.eq("memberId", records.getMemberId());
		wrapper.eq("parkId", records.getParkId());
		
		List<ChatRecords> list = baseMapper.selectList(wrapper);
		if(list != null && list.size() > 0) {
			ChatRecords records2 = list.get(0);
			if(isUser) {
				records2.setUserStatus(ChatRecordsStatus.IS_SHOW.getKey());
			}else {
				records2.setParkStatus(ChatRecordsStatus.IS_SHOW.getKey());
			}
			records2.setLastTime(new Date());
			baseMapper.updateAllColumnById(records2);
		}else {
			baseMapper.insertAllColumn(records);
		}
		
		//维护已沟通职位信息
		String jobId = records.getJobId();
		if(StringUtils.isNotEmpty(jobId)) {
			EntityWrapper<ChatJob> entityWrapper = new EntityWrapper<>();
			entityWrapper.eq("memberId", records.getMemberId());
			entityWrapper.eq("jobId", jobId);
			
			List<ChatJob> chatJobs = chatJobMapper.selectList(entityWrapper);
			if(chatJobs != null&&chatJobs.size() > 0) {
				ChatJob chatJob = chatJobs.get(0);
				chatJob.setLastTime(new Date());
				chatJobMapper.updateAllColumnById(chatJob);
			}else {
				ChatJob arg0 = new ChatJob();
				arg0.setCreateTime(new Date());
				arg0.setLastTime(new Date());
				arg0.setMemberId(records.getMemberId());
				arg0.setJobId(records.getJobId());
				chatJobMapper.insert(arg0);
			}
		}
	}

	@Override
	public Page<ChatRecordsVo> selectRecordsList(ChatRecordsSearchVo chatRecords, Page<ChatRecordsVo> page) {
		List<ChatRecordsVo> chatRecordsVos = baseMapper.selectUserRecordsListPage(chatRecords,page);
		page.setRecords(chatRecordsVos);
		return page;
	}

	@Override
	public Page<ChatRecordsVo> selectParkRecordsList(ChatRecordsSearchVo chatRecords, Page<ChatRecordsVo> page) {
		List<ChatRecordsVo> chatRecordsVos = baseMapper.selectParkRecordsListPage(chatRecords,page);
		page.setRecords(chatRecordsVos);
		return page;
	}

	@Override
	public Page<ParkResumeViewVo> selectParkResumeListPage(String parkId, Page<ParkResumeViewVo> page) {
		page.setRecords(baseMapper.selectParkResumeListPage(parkId, page));
		return page;
	}
}
